package come.iweb.user.dao;

import come.iweb.user.model.User;
import come.iweb.util.JdbcUtil;
import come.iweb.util.PasswordUtil;

import java.util.List;
import java.util.Map;
// 实现UserDao接口，提供User相关的具体方法实现

public class UserDaoImpl implements UserDao{
    @Override
    public User queryByUsernamePassword(String username, String password) {
        // 先根据用户名查询用户
        String sql = "SELECT id, username, password, role, email, " +
                "date_format(createtime,'%Y-%m-%d %H:%i:%s') as createtime, " +
                "date_format(updatetime,'%Y-%m-%d %H:%i:%s') as updatetime " +
                "FROM user WHERE username = ?";
        try {
            List<Map<String, Object>> list = JdbcUtil.jdbcBySelect(sql, username);
            if (list != null && list.size() > 0) {
                Map<String, Object> map = list.get(0);
                // 验证密码
                String storedPassword = (String) map.get("password");
                if (PasswordUtil.verifyPassword(password, storedPassword)) {
                    Object idObj = map.get("id");
                    long id = idObj instanceof Number ? ((Number) idObj).longValue() : 0L;
                    return new User(
                            id,
                            (String) map.get("username"),
                            (String) map.get("password"),
                            (String) map.get("role"),
                            (String) map.get("email"),
                            (String) map.get("createtime"),
                            (String) map.get("updatetime"),
                            null  // oldpassword字段设为null
                    );
                }
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int addOne(User user) {
        String sql = "INSERT INTO user (username, password, role, email, createtime, updatetime) " +
                "VALUES (?, ?, ?, ?, NOW(), NOW())";
        try {
            return JdbcUtil.jdbcByUpdate(sql,
                    user.getUsername(),
                    user.getPassword(),
                    user.getRole(),
                    user.getEmail());
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public User queryById(Long id) {
        String sql = "SELECT id, username, password, role, email, " +
                "date_format(createtime,'%Y-%m-%d %H:%i:%s') as createtime, " +
                "date_format(updatetime,'%Y-%m-%d %H:%i:%s') as updatetime " +
                "FROM user WHERE id = ?";
        List<Map<String, Object>> list = JdbcUtil.jdbcBySelect(sql, id);
        if (list != null && list.size() > 0) {
            Map<String, Object> map = list.get(0);
            return new User(
                    (Long) map.get("id"),
                    (String) map.get("username"),
                    (String) map.get("password"),
                    (String) map.get("role"),
                    (String) map.get("email"),
                    (String) map.get("createtime"),
                    (String) map.get("updatetime"),
                    null  // oldpassword字段设为null
            );
        }
        return null;
    }


    @Override
    public int updateUser(User user) {
        // 先验证旧密码
        String checkSql = "SELECT id FROM user WHERE id = ? AND password = ?";
        List<Map<String, Object>> list = JdbcUtil.jdbcBySelect(checkSql, user.getId(), user.getOldpassword());
        if (list.size() == 0) {
            return -1; // 旧密码错误
        }

        // 更新邮箱和密码
        String sql = "UPDATE user SET email = ?, password = ?, updatetime = NOW() WHERE id = ?";
        return JdbcUtil.jdbcByUpdate(sql, user.getEmail(), user.getPassword(), user.getId());
    }


    @Override
    public int deleteById(Long id) {
        String sql = "DELETE FROM user WHERE id=?";
        return JdbcUtil.jdbcByUpdate(sql, id);
    }
    
    @Override
    public User queryByUsername(String username) {
        String sql = "SELECT id, username, password, role, email, " +
                "date_format(createtime,'%Y-%m-%d %H:%i:%s') as createtime, " +
                "date_format(updatetime,'%Y-%m-%d %H:%i:%s') as updatetime " +
                "FROM user WHERE username = ?";
        List<Map<String, Object>> list = JdbcUtil.jdbcBySelect(sql, username);
        if (list != null && list.size() > 0) {
            Map<String, Object> map = list.get(0);
            return new User(
                    (Long) map.get("id"),
                    (String) map.get("username"),
                    (String) map.get("password"),
                    (String) map.get("role"),
                    (String) map.get("email"),
                    (String) map.get("createtime"),
                    (String) map.get("updatetime"),
                    null  // oldpassword字段设为null
            );
        }
        return null;
    }
}
